<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Payment_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;

	function Payment_model()
	{
		parent::Model();
		
		$host = $this->db->hostname;
		$arrHost = explode(':',$host);
		
		$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	
	function cod_bill_getall($uid, $permit, $provice = -1, $status = -1, $distric = -1, $type = 0, $page = 1, $limit = 20)
	{
		$data = array();
		$data['items'] = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_multi_query($iconn, "CALL selfserving_get_all_COD_bill($provice,$status,$distric,$uid,$permit,$page,$limit,$type);") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_object($result))
        {
			$data['items'][] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_object($result);
		$data['pages'] = $row->totalpage;

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function cod_bill_get($id)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_multi_query($iconn, "CALL selfserving_get_COD_bill($id);") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        $row = mysqli_fetch_object($result);

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $row;
	}
	
	function province_get_all()
	{
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_province_get_all();") or die(mysqli_error($iconn));
		while($row = mysqli_fetch_object($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function districts_get_all_by_provinceid($id)
	{
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_districts_get_all_by_provinceid($id);") or die(mysqli_error($iconn));
		while($row = mysqli_fetch_object($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function updateAction($uid, $id, $action)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_query($iconn, "CALL selfserving_COD_bill_update_status($uid,$id,$action);") or die(mysqli_error($iconn));
		mysqli_close($iconn);
	}
	
	function addMoney($cusid,$key,$money,$note,$typepay,$uid,$codid)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_query($iconn, "CALL selfserving_add_monney($cusid,'$key',$money,'$note','$typepay',$uid,$codid);") or die(mysqli_error($iconn));
		mysqli_close($iconn);
	}
	
	function getPaymentKey()
	{
		return uniqid();
	}
	
	function get_user_info($uid)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_user_info($uid);") or die(mysqli_error($iconn));
		$row = mysqli_fetch_object($result);
		
		mysqli_free_result($result);
		mysqli_close($iconn);
		
		return $row;
	}
}